package com.aluka.nirvana.framework.core.model;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aluka.nirvana.framework.core.context.SpringContextAware;
import com.aluka.nirvana.framework.core.exception.BaseException;
import lombok.Data;

import java.io.Serializable;

/**
 * 基础异常序列化模型
 * @author gongli
 * @since 2019/12/25
 */
@Data
public class BaseExceptionBody implements Serializable {
    private static final long serialVersionUID = -1270478894426234738L;
    private String code;
    private String serviceName;
    private String message;
    private String additionalMessage;
    private String stackTrace;
    private String exceptionType;

    public BaseExceptionBody() {
    }

    public BaseExceptionBody(BaseException exception) {
        this.code = exception.getErrorCode();
        this.message = exception.getMessage();
        this.serviceName = SpringContextAware.getApplicationName();
        this.exceptionType = exception.getClass().getName();
        this.additionalMessage = exception.getAdditionalMessage();
        Throwable rootCause = ExceptionUtil.getRootCause(exception);
        this.stackTrace = ExceptionUtil.stacktraceToString(rootCause, 500);

    }


    @Override
    public String toString() {
        return "BaseExceptionBody(code=" + this.getCode() + ", serviceName=" + this.getServiceName() + ", message=" + this.getMessage() + ", additionalMessage=" + this.getAdditionalMessage() + ", exceptionType=" + this.getExceptionType() + ")";
    }
}
